/**
 * Created by Administrator on 2017/1/11.
 */
$(function () {
    getUrl("myAD/applicationAD/index");
    //等待加载
    loading();
    var cookie = new sweetCookie();
    var info = cookie.getCookie("AppName");
    if (isNotEmpty(info)) {
        $("#name").val(info);
        ajaxSelectAppByCondition(1, $("#name").val());
        cookie.setCookie("AppName" , "", -1);
    } else {
        $(".loadingBox").hide();
    }
    $('#add').click(function () {
        loading();
        $("#search").empty();
        if (isNotEmpty($("#name").val())) {
            ajaxSelectAppByCondition(1, $("#name").val());
        }
    });

})
/**
 * 查找内容商（条件、首字母）
 *
 * @param page
 *            当前页码
 * @param info：查询条件
 * @param first：首字母
 * @returns
 */
function ajaxSelectAppByCondition(page, condition) {
    var url = "/Lvscore_User_M/myAds/ajaxSelectAppByCondition";
    $.post(
        url,
        {
            page: page,
            condition: condition,
        },
        function (data) {
            $("#search").empty();
            if (data.appList.length == 0) {
                $(".tcdPageCode").empty();
                $("#search").html('<div class="noApplication" style="color:#93bdf9;"><img src="/Lvscore_User_M/userWeb/static/images/dissearch.png" alt="">未搜索到结果</div>')
            } else {
                $.each(data.appList, function (i, e) {
                    $('#search').append(
                        '<div class="resultBox" >' +
                            '<div class="detailContenter inline-block left">' +
                                '<img src="' + e.logourl + '" width="100px" height="100px" class="left">' +
                                '<div class="left">' +
                                    '<p>应用名称：' + e.name + '</p>' +
                                    '<p>应用 I D ：' + e.id + '</p>' +
                                    '<p class="desc" title="'+e.longdesc+'">应用描述：' + getDesc(e.longdesc) + '</p>' +
                                '</div>' +
                                //'<span class="right add shoped"><a href="/Lvscore_User_M/userWeb/views/ADadmin/payment/buyAPP.html?appId=' + e.id + '">购买</a></span>'+
                            '</div>' +
                            '<span class="add right cared" appName="' + e.name + '" appId="' + e.id + '" cared="'+e.cared+'"> </span>' +
                        '</div>'
                    );
                });
                getAddStatus();
                $(".add_A").click(function () {
                    var this_ = $(this);
                    var id = this_.parent().attr("appId");
                    var name = this_.parent().attr("appName");
                    addApp(id + ":" + name);
                });
            }
            if (data.sumpage <= 1) {
                $(".tcdPageCode").empty().unbind();
            }
            if ($(".tcdPageCode").html() == "") {
                createPage(data.sumpage, data.nowpage, condition);
            }
            //等待加载的效果消失
            $(".loadingBox").hide();
    }, "json");
}

function createPage(pages, pageNum, info) {
    if (pages > 1) {
        $(".tcdPageCode").createPage({
            pageCount: pages,
            current: pageNum,
            backFn: function (p) {
                ajaxSelectAppByCondition(p, info);
            }
        });
    } else if (pages <= 1) {
        $(".tcdPageCode").empty().unbind();
    }
}

/**
 * 添加应用
 * @param info
 * @returns
 */
function addApp(info) {
    var cookie = new sweetCookie();
    cookie.setCookie("AppInfo", encodeURIComponent(info), 10 * 60 * 1000);
    window.location.href = '/Lvscore_User_M/userWeb/views/myAD/applicationAD/bindID.html';
}

/**
 * 查找
 * @param e
 * @returns
 */
function searchEnter(e) {
    var key = window.event ? e.keyCode : e.which;
    if (key == 13) {
        $('#add')[0].click();
    }
}

/**
 * 获得添加状态
 * @returns
 */
function getAddStatus () {
    $.each($(".cared"), function (i, e) {
        if ($(e).attr("cared") == 1) {
            $(e).html('已添加').attr("style","opacity: 0.7;pointer-events: none;")
        } else{
            $(e).html('<a class="add_A" href="javascript:;">添加</a>')
        }
    });
}

function getDesc (desc) {
    if (getLength(desc) > 40) {
        return desc.substring(0, 40) + "..."
    } else {
        return desc;
    }
}